﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace MyTRCP.Common.Services
{
    public class DevCtrlLinkService : DevBluetoothLinkService
    {
        public DevCtrlLinkService(ILogger<DevCtrlLinkService> logger, IConfigService ics) : base(logger, ics)
        {
            logger.LogDebug($"DevCtrlLinkService creating...");
        }

        protected override Guid TargetService => new Guid("00001101-0000-1000-8000-00805F9B34FB");

        protected override string TargetServiceName => "控制";

        private int payloadReadIndex = 0;
        private byte checksumFlagByte = 0;
        private byte argLen = 0;
        private ushort payloadType = 0;
        private ushort cmdId = 0;
        protected override void ProcessReceivedDataQueue()
        {
            while (_btStreamReadUnprocessDataQueue.Count > 0)
            {
                nowByte = _btStreamReadUnprocessDataQueue.Dequeue();
                if (payloadRecvUncomplete)
                {
                    uncompletePayload.Add(nowByte);
                    payloadReadIndex++;
                    if (payloadReadIndex == (8 + argLen + ((checksumFlagByte & 1) != 0 ? 1 : 0)))
                    {
                        payloadRecvUncomplete = false;
                        _btStreamReadCompletelyPayloadQueue.Add(uncompletePayload.ToArray());
                        uncompletePayload.Clear();
                        payloadReadIndex = 0;
                        checksumFlagByte = 0;
                        argLen = 0;
                        payloadType = 0;
                        cmdId = 0;
                    }
                }
                else
                {
                    switch (payloadReadIndex)
                    {
                        case 0:
                            if (nowByte == 0xff)
                            {
                                payloadReadIndex++;
                            }
                            else
                            {
                                payloadReadIndex = 0;
                            }
                            break;
                        case 1:
                            if (nowByte == 0x01)
                            {
                                payloadReadIndex++;
                            }
                            else
                            {
                                payloadReadIndex = 0;
                            }
                            break;
                        case 2:
                            checksumFlagByte = nowByte;
                            payloadReadIndex++;
                            break;
                        case 3:
                            argLen = nowByte;
                            if (argLen <= 254)
                            {
                                payloadReadIndex++;
                            }
                            else
                            {
                                checksumFlagByte = 0;
                                argLen = 0;
                                payloadReadIndex = 0;
                            }
                            break;
                        case 4:
                            payloadType = (ushort)(nowByte << 8);
                            payloadReadIndex++;
                            break;
                        case 5:
                            payloadType |= nowByte;
                            payloadReadIndex++;
                            break;
                        case 6:
                            cmdId = (ushort)(nowByte << 8);
                            payloadReadIndex++;
                            break;
                        case 7:
                            cmdId |= nowByte;
                            payloadReadIndex++;
                            payloadRecvUncomplete = true;
                            uncompletePayload.AddRange(new byte[] { 0xff, 0x01, checksumFlagByte, argLen, (byte)(payloadType >> 8), (byte)(payloadType & 0xff), (byte)(cmdId >> 8), (byte)(cmdId & 0xff) });
                            if (payloadReadIndex == (8 + argLen + ((checksumFlagByte & 1) != 0 ? 1 : 0)))
                            {
                                payloadRecvUncomplete = false;
                                _btStreamReadCompletelyPayloadQueue.Add(uncompletePayload.ToArray());
                                uncompletePayload.Clear();
                                payloadReadIndex = 0;
                                checksumFlagByte = 0;
                                argLen = 0;
                                payloadType = 0;
                                cmdId = 0;
                            }
                            break;
                    }
                }
            }
        }
    }
}
